<?php
namespace Admin\Controller;

class AppTestController extends \Admin\Controller\AdminController
{
	public function test()
	{
		$value = global_config('default_picture');
		//$list = S(C('CONFIG_CACHE_NAME'));
		//echo json_encode($list);die();
		echo json_encode($value);
	}
	
	public function apiCacheTest()
	{
		$list = S(C('API_CACHE_NAME'));
		echo json_encode($list);
	}
	
	public function configCacheTest()
	{
		$res = global_config('user_token_expire');
		die($res);
	}
	
	/**
	 * 添加文件测试
	 * @author 刘新
	 */
	public function addFile()
	{
		$fileType = I('file_type', '', '/^\d+$/');
		!$fileType && $this->error('参数错误');
		$this->assign('_title', '新增文件');
		$this->assign('fileType', C('FILE_TYPE'));
		$this->assign('fileLimit', file_limit('DOWNLOAD_UPLOAD'));
		$this->display('add_edit');
	}
	
	/**
	 * API测试
	 */
	public function apiTest()
	{
		/* $p = array('apiCode'=>'_fans_list_001', 'param'=>array('userToken'=>'必传','page'=>'页码，可选','size'=>'每页显示多少，可选'));
		die(json_encode($p));    */
		//GET请求显示测试页面
		if(IS_GET) {
			//获取接口代码
			$apiCode = I('apiCode');
			if(!$apiCode) $this->error('参数错误');
			
			//查询接口
			$api = $this->returnOne('Api', array('code'=>$apiCode));
			if(!$api) $this->error("{$api}不存在");
			
			//判断JSON格式参数说明
			if(!$api['params_json']) $this->error("{$api}信息不全");
			
			$api['method_text'] = C('API_REQUEST')[$api['method']];
			$api['apiCode'] = $api['code'];
			$this->assign('_title', '接口测试');
			$this->assign('param', $api);
			$this->display('api_test');
		}
		
		//接口参数与请求方式
		$param = I('param');
		if(!$param) $this->ajaxReturn(array('error' => 1, 'msg' => '参数错误1'));
		$method = I('method');
		if(!$method) $this->ajaxReturn(array('error' => 1, 'msg' => '参数错误'));
		
		//远程请求接口
		$res = \Common\Util\Util::curl(array('json'=>$param), array('method' => $method));
		
		//判断请求结果
		if(!$res) $this->ajaxReturn(array('error' => 1, 'msg' => '远程请求失败'));
		die($res);
	}
	
	/**
	 * 生成JSON字符串
	 * @author 刘新
	 */
	public function paramJson()
	{
		//获取参数
		$param = I('param');
		if(!$param) $this->ajaxReturn(array('error' => 1, 'msg' => '参数错误'));
		
		//转换成数组
		$param = json_decode($param, true);
		if(!$param) $this->ajaxReturn(array('error' => 1, 'msg' => '参数格式错误'));
		unset($param['token']);
		
		//设置密码
		if($param['param']['loginPass']) $param['param']['loginPass'] = hash('md5', $param['param']['loginPass']);
		if($param['param']['oldPwd']) $param['param']['oldPwd'] = hash('md5', $param['param']['oldPwd']);
		if($param['param']['newPwd']) $param['param']['newPwd'] = hash('md5', $param['param']['newPwd']);
		if($param['param']['sureNewPwd']) $param['param']['sureNewPwd'] = hash('md5', $param['param']['sureNewPwd']);
		
		$str = api_sign($param, true);
		$token = api_sign($param);
		$param['token'] = $token;
		$param['param'] = json_encode($param, JSON_UNESCAPED_UNICODE);
		$param['signStr'] = $str;
		$this->ajaxReturn(array('error' => 0, 'msg' => $param));
		//$param = array('param' => $param, 'sign_str' => $str);
		//$this->ajaxReturn(array('error' => 0, 'msg' => $param));
		//$this->ajaxReturn(array('error' => 0, 'msg' => json_encode($param, JSON_UNESCAPED_UNICODE)));
	}
}